using System.Linq;
using Home.Site.Models.DataLayer;
using Home.Site.Models.WebUI;

namespace Home.Site.Models.Account
{
    public class AccountViewModelBuilder : IAccountViewModelBuilder
    {

        private readonly IAccountRepository accountRepository;
        private readonly IReviewRepository reviewRepository;
        private readonly IPhotoAlbumRepository photoAlbumRepository;
        private readonly IMusicAlbumsRepository musicAlbumsRepository;

        public AccountViewModelBuilder(IReviewRepository reviewRepository, 
            IPhotoAlbumRepository photoAlbumRepository, 
            IMusicAlbumsRepository musicAlbumsRepository, 
            IAccountRepository accountRepository)
        {
            this.reviewRepository = reviewRepository;
            this.photoAlbumRepository = photoAlbumRepository;
            this.musicAlbumsRepository = musicAlbumsRepository;
            this.accountRepository = accountRepository;
        }

        public UserViewModel Build(LocalPasswordModel user)
        {
            return new UserViewModel
                {
                    IsRoot = user.IsRoot,
                    IsAdmin = user.IsAdmin,
                    IsOperator = user.IsOperator,
                    Name = user.ShowName,
                    Email = user.Email,
                    Other = user.Other,
                    Reviews = reviewRepository.GetReviewByUser(user.UserName),
                    PhotoAlbums = photoAlbumRepository.GetPhotoAlbumsByUser(user.UserName),
                    MusicAlbums = musicAlbumsRepository.GetMusicAlbumsByUser(user.UserName),
                    Gravatar = user.Gravatar,
                    Login = user.UserName,
                    ReviewsCount = reviewRepository.All.Count(r => r.CreateUser == user.UserName)
                };
        }
    }
}